package com.xiaoyun.controller;

import com.xiaoyun.constant.Const;
import com.xiaoyun.pojo.Items;
import com.xiaoyun.pojo.ItemsImg;
import com.xiaoyun.pojo.ItemsParam;
import com.xiaoyun.pojo.ItemsSpec;
import com.xiaoyun.service.ItemService;
import com.xiaoyun.utils.PagedGridResult;
import com.xiaoyun.utils.XYJSONResult;
import com.xiaoyun.vo.CommentLevelCountsVO;
import com.xiaoyun.vo.ItemInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("商品接口")
@RestController
@RequestMapping("items")
@RequiredArgsConstructor
public class ItemController extends BaseController {

    private final ItemService itemService;

    @ApiOperation("查询商品详细")
    @GetMapping("/info/{itemId}")
    public XYJSONResult subCat(@PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return XYJSONResult.errorMsg("");
        }

        Items items = itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgs = itemService.queryItemImgList(itemId);
        ItemsParam itemsParam = itemService.queryItemParam(itemId);
        List<ItemsSpec> itemsSpecs = itemService.queryItemSpecList(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(itemsImgs);
        itemInfoVO.setItemParams(itemsParam);
        itemInfoVO.setItemSpecList(itemsSpecs);

        return XYJSONResult.ok(itemInfoVO);
    }

    @ApiOperation("查询商品评价等级")
    @GetMapping("/commentLevel")
    public XYJSONResult commentLevel(@RequestParam String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return XYJSONResult.errorMsg("");
        }
        CommentLevelCountsVO countsVO = itemService.queryCommentCounts(itemId);
        return XYJSONResult.ok(countsVO);
    }

    @ApiOperation("查询商品评价等级")
    @GetMapping("/comment")
    public XYJSONResult commentLevel(
            @RequestParam String itemId,
            @RequestParam Integer level,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = Const.COMMENT_PAGE_SIZE) Integer pageSize) {

        if (StringUtils.isBlank(itemId)) {
            return XYJSONResult.errorMsg("");
        }
        PagedGridResult grid = itemService.queryPagedComments(itemId, level, page, pageSize);
        return XYJSONResult.ok(grid);
    }


    @ApiOperation("搜索商品列表")
    @GetMapping("/search")
    public XYJSONResult search(
            @RequestParam String keywords,
            @RequestParam String sort,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = Const.PAGE_SIZE) Integer pageSize) {

        if (StringUtils.isBlank(keywords)) {
            return XYJSONResult.errorMsg("");
        }
        PagedGridResult grid = itemService.searchItems(keywords, sort, page, pageSize);
        return XYJSONResult.ok(grid);
    }

    @ApiOperation("通过分类id搜索商品列表")
    @GetMapping("/catItems")
    public XYJSONResult catItems(
            @RequestParam Integer catId,
            @RequestParam String sort,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = Const.PAGE_SIZE) Integer pageSize) {

        if (catId == null) {
            return XYJSONResult.errorMsg("");
        }
        PagedGridResult grid = itemService.searchItems(catId, sort, page, pageSize);
        return XYJSONResult.ok(grid);
    }

    @ApiOperation("根据商品规格ids查询最新的商品数据")
    @GetMapping("/refresh")
    public XYJSONResult refresh(@RequestParam String itemSpecIds) {
        if (StringUtils.isBlank(itemSpecIds)) {
            return XYJSONResult.errorMsg("");
        }
        List<ItemsSpec> list = itemService.queryItemSpecList(itemSpecIds);
        return XYJSONResult.ok(list);
    }


}
